package net.mihome.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import net.mihome.app.R;

/**
 * Created by zhao on 2015/3/2.
 */
public class MiButton extends Button {

    private int referenceWidth;
    private int referenceHeight;
    public MiButton(Context context) {
        super(context);
        initView();
    }

    public MiButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MiButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();

    }
    /**
     * */
    public void initView(){
        Drawable referenceDrawable = getResources().getDrawable(R.drawable.smiley_add_btn);
        referenceWidth = referenceDrawable.getIntrinsicWidth();
        referenceHeight = referenceDrawable.getIntrinsicHeight();
    }
     /**
     * */
    public void setBackageFromHttp(String url){
                    ImageLoader.getInstance().loadImage(url,new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String s, View view) {

                        }

                        @Override
                        public void onLoadingFailed(String s, View view, FailReason failReason) {
                            Drawable avatar =getResources().getDrawable(R.drawable.contact_common);
                            avatar.setBounds(0, 0, referenceWidth, referenceHeight);
                            setCompoundDrawables(null, avatar, null, null);
                        }

                        @Override
                        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                            Drawable avatar =new BitmapDrawable(bitmap);
                            avatar.setBounds(0, 0, referenceWidth, referenceHeight);
                            setCompoundDrawables(null, avatar, null, null);
                        }

                        @Override
                        public void onLoadingCancelled(String s, View view) {

                        }
                    });
    }
}
